package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.ProjectExecutionProgress;
import com.mxpio.erp.common.equipment.entity.ProjectManagement;
import com.mxpio.erp.common.equipment.service.ProjectExecutionProgressService;
import com.mxpio.erp.common.equipment.service.ProjectManagementService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "ProjectExecutionProgressController", description = "执行进度列表")
@RestController
@RequestMapping("/erp/equipment/project/execution/progress/")
public class ProjectExecutionProgressController {

    @Autowired
    private ProjectExecutionProgressService projectExecutionProgressService;

    @Autowired
    private ProjectManagementService projectManagementService;

    @GetMapping("list")
    @Operation(summary = "执行进度列表", description = "获取执行进度列表(无分页)", method = "GET")
    public Result<List<ProjectExecutionProgress>> list(@Parameter(description = "查询参数")Criteria criteria){
        List<ProjectExecutionProgress> list = projectExecutionProgressService.list(ProjectExecutionProgress.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "执行进度列表", description = "获取执行进度列表(分页)", method = "GET")
    public Result<Page<ProjectExecutionProgress>> list(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<ProjectExecutionProgress> listPage = projectExecutionProgressService.listPage(ProjectExecutionProgress.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据proCode获取执行进度", description = "根据proCode获取执行进度(无分页)", method = "GET")
    public Result<ProjectExecutionProgress> getId(@PathVariable(name = "proCode", required = true) String proCode){
        ProjectExecutionProgress byId = projectExecutionProgressService.getById(ProjectExecutionProgress.class, proCode);
        return Result.OK(byId);
    }

    @RequestMapping("add/{proCode}")
    @Operation(summary = "保存执行进度", description = "保存执行进度", method = "POST")
    public Result<ProjectExecutionProgress> add(@PathVariable(name = "proCode", required = true) String proCode, @RequestBody ProjectExecutionProgress projectExecutionProgress){
        Criteria criteria = Criteria.create();
        criteria.addCriterion("proCode", Operator.EQ,proCode);
        ProjectManagement projectManagement = projectManagementService.getById(ProjectManagement.class, proCode);
        if (projectManagement.getSchedule() < 100){
            String proStatus = projectManagement.getProStatus();
            if (proStatus.equals(EquipmentEnums.Project.IMPROGRESS.getCode())){
                // 100减总进度, 拿到还能增加多少进度的值
                int aged = 100 - projectManagement.getSchedule();
                // 增加
                if (aged >= projectExecutionProgress.getItemSpeed()){// 拿到将增加的进度
                    projectExecutionProgress.setItemTime(new Date());
                    projectExecutionProgressService.save(projectExecutionProgress);
                    // 拿所有的进度明细
                    List<ProjectExecutionProgress> ProjectExecutionProgress = projectExecutionProgressService.list(ProjectExecutionProgress.class, criteria);
                    // 总进度
                    int itemSpeed = 0;
                    for (ProjectExecutionProgress executionProgress : ProjectExecutionProgress) {
                        Integer itemSpeed1 = executionProgress.getItemSpeed();
                        itemSpeed += itemSpeed1;
                    }
                    if (itemSpeed > 100){
                        return Result.error("总进度超过100%, 不可增加进度");
                    }else {
                        projectManagement.setSchedule(itemSpeed);
                        projectManagementService.update(projectManagement);
                        // 总进度达到100%, 生成实际结束时间
                        if (projectManagement.getSchedule() == 100 && projectManagement.getProStatus().equals(EquipmentEnums.Project.IMPROGRESS.getCode())){
                            projectManagementService.updateRealyEndTime(projectManagement, EquipmentEnums.Project.COMPLETED);
                        }
                        return Result.OK();
                    }
                }
                return Result.error("不符合总进度, 不可增加执行进度");
            }
        }
        return Result.error("该项目已完成或状态不在执行中, 不可增加进度");
    }

    @PutMapping("edit")
    @Operation(summary = "编辑执行进度", description = "编辑执行进度", method = "PUT")
    public Result<ProjectExecutionProgress> edit(@RequestBody ProjectExecutionProgress projectExecutionProgress){
        ProjectExecutionProgress update = projectExecutionProgressService.update(projectExecutionProgress);
        return Result.OK(update);
    }

    @DeleteMapping("remove/{ids}/{proCode}")
    @Operation(summary = "根据id删除执行进度", description = "删除执行进度,多个进度用英文逗号分隔", method = "DELETE")
    public Result<ProjectExecutionProgress> remove(@PathVariable(name = "ids", required = true)String ids, @PathVariable(name = "proCode", required = false) String proCode){
        String[] id = ids.split(",");
        for (String key : id) {
            ProjectManagement projectManagement = projectManagementService.getById(ProjectManagement.class, proCode);
            if (projectManagement.getProStatus().equals(EquipmentEnums.Project.COMPLETED.getCode())){
                return Result.error("状态已完成, 不可删除");
            }else {
                projectExecutionProgressService.remove(ProjectExecutionProgress.class, key);
            }
            Criteria criteria = Criteria.create();
            criteria.addCriterion("proCode", Operator.EQ,proCode);
            List<ProjectExecutionProgress> ProjectExecutionProgress = projectExecutionProgressService.list(ProjectExecutionProgress.class, criteria);
            int itemSpeed = 0;
            for (ProjectExecutionProgress projectExecutionProgress : ProjectExecutionProgress) {
                Integer itemSpeed1 = projectExecutionProgress.getItemSpeed();
                itemSpeed += itemSpeed1;
            }
            ProjectManagement pmList = projectManagementService.getById(ProjectManagement.class, proCode);
            pmList.setSchedule(itemSpeed);
            projectManagementService.update(pmList);
        }
        return Result.OK();
    }
}
